package com.yxwl.demo.ui.print.printer;


import android.util.Log;

import com.print.base.bean.PrinterConstantPool;
import com.print.base.bean.TaskCallBean;
import com.print.base.listen.TaskCallback;
import com.print.printer.Command;
import com.print.printer.PrintImgHelper;
import com.print.printer.Printer;
import com.yxwl.demo.ui.print.PrintActivity;
import com.yxwl.demo.utils.StringUtil;
import com.yxwl.demo.widget.CustomProgress;
import com.yxwl.demo.widget.LogView;


/**
 * A80
 * 300点设备 打印最大宽度2496像素
 * 200点设备 打印最大宽度1664
 *
 * C80打印机主动回调状态
 * 0XFF 0X01 缺纸
 * 0XFF 0X02 开盖
 * 0XFF 0X03 过热
 * 0XFF 0X04 低电量
 *
 *
 * 打印回调
 * 1连续纸 0XAA
 * 2非连续纸 0X4B 0X4F （注意：打印结束如果打印机缺纸，就会返回 0x4F 0x4B 0x54 0x41 0x4F 0x4B）
 * 非连续纸打印结束，如果结束会返回下面的情况
 * 0x4F 0x4B 0x54 0x41 0x4F 0x4B 或 0x4F 0x4B 0x4F 0x4B 0x54 0x41 0x4F 0x4B 或其他
 * 基本都是0x4F 0x4B 开头 ，中间穿插 0x54 0x41，然后0x4F 0x4B结尾
 *
 * C80 D810打印机纸张类型不要设置间隙纸模式 PrinterConstantPool.PaperType.GAP
 * 否则会打印异常
 */
public class A80Util extends PrintImgUtil {


    public A80Util(Printer printer) {
        super(printer);
    }

    /**
     * A80 连续纸打印回调是0XAA
     *     间隙纸 黑标纸 回调0X4F 0X4B
     */
    @Override
    protected void printReadCall(TaskCallBean bean) {
        if (bean.data == null) {
            return;
        }

        if (bean.data[0] == (byte) 0XAA || (bean.data[0] == (byte) 0X4F && bean.data[1] == (byte) 0X4B)) {
            if (count < all) {
                count++;
                LogView.log("正在打印第" + count + "张图片");
                CustomProgress.updatePD("正在打印第" + count + "张图片");
                if (!check()) {
                    print();
                }
            } else {
                stopPrint();
            }

        }
    }

    @Override
    protected boolean print(TaskCallBean bean) {
        if (super.print(bean)) {
            return true;
        }
        printer.addTask(Command.set_Speed(speed), null, false,null);
        printer.addTask(Command.set_Density(density), null, false, new TaskCallback() {
            @Override
            public void sendStatus(TaskCallBean bean) {
                if (bean.status != PrinterConstantPool.Status.OK) {
                    LogView.log("发送失败,自动结束打印 " + bean.msg);
                    CustomProgress.dismissPD();
                }
            }

            @Override
            public void readCall(TaskCallBean bean) {
                print();
            }

            @Override
            public int timeOut() {
                return 1000;
            }
        });
        return true;
    }

    protected void print() {
        if (check()) {
            return;
        }

        PrintImgHelper.PrintBuild build = helper.build(printCall);
        build.enable();
        boolean isGap = true;
        switch (paperType) {
            case PrinterConstantPool.PaperType.CONTINUOUS:
                build.paperType(16);
                isGap = false;
                break;
            case PrinterConstantPool.PaperType.GAP:
                // 连续纸 16;  缝隙纸 32;   黑标纸 48;
                build.paperType(32);
                break;
            case PrinterConstantPool.PaperType.BLACK:
                build.paperType(48);
                break;
            case PrinterConstantPool.PaperType.TATTOO:
                build.paperType(64);
                break;
        }
        build.CreatePage(paperW, paperH);//A80连续纸高度无需设置且设置无效，可以随便填写数值
        if (isGap && list.size() == all) {
            build.backoffPaper();
        }
        build.printImg(list.remove(0));
        if (isGap) {
            build.fixedPoint();
            if (list.size() == 0) {
                build.forwardPaper();
            }
        } else {
            build.printLinedots(60);
        }
        build.disenable();
        helper.run(build);
    }

}
